package xyz.hubery.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.hubery.domain.CoinConfig;
import xyz.hubery.model.R;
import xyz.hubery.service.CoinConfigService;

@RestController
@RequestMapping("/coinConfigs")
@Api("币种配置管理接口")
public class CoinConfigController {

    @Autowired
    private CoinConfigService coinConfigService;

    @GetMapping("/info/{id}")
    @ApiOperation("查询币种配置信息")
    public R<CoinConfig> findById(@PathVariable("id") Long id){
        CoinConfig coinConfig = coinConfigService.getById(id) ;
        return R.ok(coinConfig) ;
    }

    @PatchMapping
    @ApiOperation(value = "币种配置的修改操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coinConfig" ,value ="coinConfig 的 json 数据" )
    })
    public R update(@RequestBody @Validated CoinConfig coinConfig){
        boolean saveOrUpdate = coinConfigService.updateOrSave(coinConfig) ;
        if(saveOrUpdate){
            return R.ok() ;
        }
        return R.fail("修改失败") ;
    }
}
